<?php
namespace xmi_shop\admin\api;
use xmi_shop\controller;

use xmi_shop\admin\model\orderModel;

class order extends controller{

    public function page_list(){

        $this->template( 'sale/order/list' );

    }

    public function page_edit(){

        $this->template( 'sale/order/edit' );

    }

    public function page_show(){

        $this->template( 'sale/order/show' );

    }

    public function index(){

        $filter = [];
        $list = orderModel::get_list( $filter );

        echo json_encode( $list );

    }

    /**
     * 获取单条 订单信息
     */
    public function info(){

        $order_id = $_GET['order_id'];
        $order_info = orderModel::get_order( $order_id );

        $order_info['payment_method_text'] = $order_info['payment_method'] == 'wechat'?'微信支付':'';
        $order_info['payment_method_text'] = $order_info['payment_method'] == 'alipay'?'支付宝':$order_info['payment_method_text'];

        if( $order_info['shipping_method'] == 'package' ){
            $order_info['shipping_method_text'] = count($order_info['package']) ?$order_info['package'][0]['name']:'其他快递';
        }else if( $order_info['shipping_method'] == 'pickup' ) {
            $order_info['shipping_method_text'] = '自提订单';
        }

        echo json_encode( $order_info );

    }

    public function status_list(){

        $order_all_status = orderModel::get_all_status();

        echo json_encode( $order_all_status );
    }

    //todo 改变订单状态(添加订单历史)
    public function add_history(){

        $order_id = $_GET['order_id'];

        $order_status = $_POST['order_status'];

        $comment = $_POST['comment'];


        $data = [
            'order_status'=>$order_status,
            'comment'=>$comment,
            'create_time'=>date("Y-m-d H:i:s")
        ];
        $res = orderModel::add_history( $order_id, $data );

        echo json_encode( $res );

    }

    public function set_order_package(){

        $res = [
            'status' => 0
        ];

        $order_id = $_GET['order_id'];
        $package_no = $_POST['package_no'];
        $express_code = $_POST['express_code'];

        $data = [
            'package_no'=>$package_no,
            'express_code'=>$express_code,
        ];


        orderModel::removePackage( $order_id );
        $res['ret'] = orderModel::addPackage( $order_id, $data );

        echo json_encode( $res );

    }

}
